package com.itheima.controller.impl;

import com.itheima.controller.TeacherController;
import com.itheima.domain.Teacher;

import java.util.ArrayList;

public class TeacherControllerImpl implements TeacherController {
    @Override
    public void start() {
        while (true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choose = sc.nextLine();
            switch (choose){
                case "1":
                    addTeacher();
                    break;
                case "2":
                    delectTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllStudnet();
                    break;
            }

        }
    }

    @Override
    public void addTeacher() {
        while (true){
            System.out.println("请输入要添加的老师id:");
            String id = sc.nextLine();
            int index = teacherService.findTeacherById(id);
            if (index == -1){
                System.out.println("请输入要添加的老师姓名:");
                String name = sc.nextLine();
                System.out.println("请输入要添加的老师年龄:");
                String age = sc.nextLine();
                System.out.println("请输入要添加的老师生日:");
                String birthday = sc.nextLine();
                teacherService.addTeacher(new Teacher(id,name,age,birthday));
                System.out.println("添加成功");
                break;
            }else{
                System.out.println("该id不存在,请重新输入");
            }
        }

    }

    @Override
    public void delectTeacher() {
        //while (true){
            System.out.println("请输入要删除的老师id:");
            String id = sc.nextLine();
            int index = teacherService.findTeacherById(id);
            if (index != -1){
               teacherService.delectTeacher(index);
                System.out.println("删除成功");
            }else{
                System.out.println("该id不存在");
            }
       // }
    }

    @Override
    public void findAllStudnet() {
        ArrayList<Teacher> allTeacher = teacherService.findAllTeacher();
        if (allTeacher.size() == 0){
            System.out.println("当前系统无数据,请先添加");
        }else {
            System.out.println("学号\t\t姓名\t年龄\t\t生日");
            for (Teacher stu : allTeacher) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    @Override
    public void updateTeacher() {
        while (true){
            System.out.println("请输入要修改的老师id:");
            String id = sc.nextLine();
            int index = teacherService.findTeacherById(id);
            if (index != -1){
                System.out.println("请输入要修改的老师姓名:");
                String name = sc.nextLine();
                System.out.println("请输入要修改的老师年龄:");
                String age = sc.nextLine();
                System.out.println("请输入要修改的老师生日:");
                String birthday = sc.nextLine();
                teacherService.updateTeacher(index,new Teacher(id,name,age,birthday));
                System.out.println("修改成功");
                break;
            }else{
                System.out.println("该id已存在,请重新输入");
            }
        }
    }
}
